package com.chicken.controller;

import com.chicken.util.Result;
import org.apache.shiro.ShiroException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class ExceptionController {


    @ExceptionHandler(ShiroException.class)
    public Result handle401(ShiroException e) {
        return Result.fail("401", e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handle402(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
        return Result.fail("402", objectError.getDefaultMessage());
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler403(IllegalArgumentException e) {
        return Result.fail("403",e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result handler(RuntimeException e) {
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }


}
